package cnu.ldc.jdbctest;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import cnu.ldc.model.User;
import cnu.ldc.util.ResultSetHandler;

public class UserTablePerationForJdbcTemplate {
public List<User> queryAll0(){
	String sql="select * from users";     
	List<User> list=JdbcTemplate.query(sql, (rs)->{
		List<User>  list0=new ArrayList<>();
		try{
			while(rs.next()) {
			User u=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
					LocalDateTime.now());
			list0.add(u);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return list0;
		
	},new Object[] {} );
	return list;
}
public List<User> queryAll1(){
	String sql="select * from users";
	List<User> list=JdbcTemplate.query(sql, new ResultSetHandler<List<User>>() {
		public List<User> handler(ResultSet rs){
		List<User>  list0=new ArrayList<>();
		try{
			while(rs.next()) {
			User u=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
					LocalDateTime.now());
			list0.add(u);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return list0;
		}
	},new Object[] {} );
	return list;
}
public List<User> queryAll2() {
	String sql="select * from users";
	List<User> list=JdbcTemplate.query(sql,new User2List(),new Object[] {});
	return list;
	}
public int addUser(User user) {
	String sql="insert into users(id,name,password,sex) values(users_id.nextval,?,?,?)";
	return JdbcTemplate.update(sql, user.getName(),user.getPassword(),user.getSex());
}
}
